﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;

namespace WPF_DependencyProperty_Demo
{
    public class MainViewModel : DependencyObject
    {

        // 定义依赖属性
        public static readonly DependencyProperty PercentageProperty =
            DependencyProperty.Register(
                "Percentage",
                typeof(int),
                typeof(MainViewModel),
                new FrameworkPropertyMetadata(
                    0,                  // 默认值
                    null,               // 值改变时回调（这里不用）
                    CoercePercentage    // 强制值合法性的回调
                )
            );

        // CLR 属性包装
        public int Percentage
        {
            get => (int)GetValue(PercentageProperty);
            set => SetValue(PercentageProperty, value);
        }

        private static object CoercePercentage(DependencyObject d, object baseValue)
        {
            int value = (int)baseValue;
            if (value < 0) return 0;
            if (value > 100) return 100;
            return value;
        }

        public MainViewModel() { }

        public void Save() 
        {

        }
    }
}
